package com.niit.galsearch.service.imp;

// SearchServiceImpl.java

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.niit.dao.GameDao;
import com.niit.entity.Game;
import com.niit.galsearch.service.SearchService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {
@Resource
    GameDao gameDao;


//    @Override
//    public List<Game> getRecommendedGames() {
//        return  gameDao.;
//    }

    @Override
    public List<Game> search(Game game1) {
        return gameDao.queryByGame(game1);
    }




    public List<Game> sample() {
        List<Game> gameList = gameDao.queryAll();
        int n = 3; // 假设需要随机选择的条数是 3
        List<Game> selectedGameList = new ArrayList<>();

        if (n >= gameList.size()) {
            return gameList; // 如果需要取出的数据条数大于等于 gameList 的长度，直接返回全部数据
        }

        Random random = new Random();
        for (int i = 0; i < n; i++) {
            int randomIndex = random.nextInt(gameList.size());
            Game randomGame = gameList.get(randomIndex);
            selectedGameList.add(randomGame);
            gameList.remove(randomIndex); // 避免重复选择同一条数据
        }

        return selectedGameList;
    }



    @Override
    public  List<Game> searchGamesByName(String gameName) {

        return gameDao.queryByName(gameName);
    }

    @Override
    public Game searchGamesById(Integer id) {
        return gameDao.queryById(id);
    }

    @Override
    public List<Game> searchGamesByCollect(Integer collect) {
        return gameDao.queryByCollect(collect);
    }

    @Override
    public List<Game> searchGamesByUpvote(Integer upvote) {
        return gameDao.queryByUpvote(upvote);
    }

    @Override
    public List<Game> searchGamesByLabel(String label) {
        return gameDao.queryByLabel(label);
    }

    @Override
    public List<Game> searchGamesByPublisher(String publisher) {
        return gameDao.queryByPublisher(publisher);
    }
}
